package com.spera.genutil;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Properties;

import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.logging.Log;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Plugin;
import org.apache.ibatis.plugin.Signature;

import com.spring.tools.MyTools;

@Intercepts({@Signature(method = "update", type = Executor.class, args = {MappedStatement.class, Object.class})})
public class MyBatisIntercept implements Interceptor{

	  // private static final Logger LOGGER = LoggerFactory.getLogger(LogInterceptor.class);

	    private Properties properties;

	    public Object intercept(Invocation invocation) throws Throwable {
	        Object[] args = invocation.getArgs();
	        // 获取执行的方法
	        if (args.length > 1) {
	            // 传入的对象
	            Object obj = args[1];
	            if (obj instanceof Log) {
	                // 若是日志对象 则直接跳过
	                return invocation.proceed();
	            }
	            saveLog(args[0], obj);
	        }
	        return invocation.proceed();
	    }

	    private void saveLog(Object arg, Object obj) throws Exception {

	        MappedStatement mappedStatement = (MappedStatement) arg;
	        // 执行的方法名
	        String name = mappedStatement.getSqlCommandType().name();
	        //String change = JsonMapper.toJson(obj);
	        
	        System.out.println(name);
	        System.out.println(obj);
	        System.out.println(obj.getClass().getSimpleName());
	        
/*			Method method = obj.getClass().getMethod("getToken");
			String token = MyTools.ObjectToString(method.invoke(obj));*/ 
	        
	//        UPDATE
			//Field[] fields = obj.getClass().getDeclaredFields();  
			Field[] fields = obj.getClass().getDeclaredFields();
		      
		    for (Field field : fields)  {
	        String varName = field.getName();  

            boolean access = field.isAccessible();  
            if(!access) field.setAccessible(true);  
               
            Object o = field.get(obj);  
            System.out.println("变量： " + varName + " = " + o);  
              
            if(!access) field.setAccessible(false); 
		    }
	        
/*	        com.spring.daily.web.model.WeeklyRecordWithBLOBs@1c9813a9
	        WeeklyRecordWithBLOBs*/
	        
	        
/*	        if (name.startsWith("INSERT")) {
	            log.setType("新增" + obj.getClass().getSimpleName());
	            log.setNewContent(change);
	        } else if (name.startsWith("UPDATE")) {
	            log.setType("修改" + obj.getClass().getSimpleName());
	            log.setNewContent(change);
	        } else if (name.startsWith("DELETE")) {
	            log.setType("删除" + obj.getClass().getSimpleName());
	            log.setOldContent(change);
	        }*/


	    }

	    public Object plugin(Object target) {
	        return Plugin.wrap(target, this);
	    }

	    public void setProperties(Properties properties) {
	        this.properties = properties;
	    }

}


